package com.biyesheji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.biyesheji.domain.Orders;
import com.biyesheji.dao.OrdersMapper;
import com.biyesheji.domain.Wares;
import com.biyesheji.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biyesheji.utils.OrdersTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author 崔
 * @since 2023-02-12
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements IOrdersService {




     /* 根据 (id, 商品名称, 地址,  状态) 条件，得到民宿商品 */
    @Override
    public List<Orders> getUserOrders(Long id, String idNumber, Long waresId, Long userPhone, String useTime, Integer status) {

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(id != null ,Orders::getId, id);
        queryWrapper.like(idNumber != null,Orders::getIdNumber, idNumber);
        queryWrapper.like(waresId != null && waresId != -1,Orders::getWaresId, waresId);


        queryWrapper.eq(userPhone != null && userPhone != -1,Orders::getUserPhone, userPhone);
        queryWrapper.eq(useTime != null,Orders::getUseTime, useTime);
        queryWrapper.eq(status != null && status != 4,Orders::getStatus, status);
        queryWrapper.orderByDesc(Orders::getUpdateTime);
        List<Orders> Orderss = this.list(queryWrapper);



        return Orderss;
    }

     /* 根据 (id, 商品名称, 地址,  状态) 条件，得到民宿商品 */
    @Override
    public List<Orders> getBusinessOrders(Long id, String idNumber, Long waresId, Long userPhone, String useTime, Integer status) {

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(id != null,Orders::getId, id);
        queryWrapper.like(idNumber != null,Orders::getIdNumber, idNumber);
        queryWrapper.eq(waresId != null && waresId != -1,Orders::getWaresId, waresId);


        queryWrapper.like(userPhone != null && userPhone != -1,Orders::getUserPhone, userPhone);
        queryWrapper.eq(useTime != null,Orders::getUseTime, useTime);
        queryWrapper.eq(status != null && status != 4,Orders::getStatus, status);
        queryWrapper.orderByDesc(Orders::getUpdateTime);
        List<Orders> Orderss = this.list(queryWrapper);



        return Orderss;
    }







    /* 通过输入的条件,统计数据的总数 */
    @Override
    public Integer getCount(Long id, String useTime, Integer status) {

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(id != null, Orders::getId, id);
        queryWrapper.eq(useTime != null, Orders::getUseTime, useTime);
        queryWrapper.eq(status != null, Orders::getStatus, status);

        int count = this.count(queryWrapper);
        return count;
    }

    // 根据订单 id 修改状态
    @Override
    public boolean updateOrders(Long id, Integer status) {


        Orders orders = new Orders();
        orders.setStatus(status);
        orders.setId(id);

        boolean b = this.updateById(orders);
        return b;
    }


    /* 初始化过期的订单 */
    @Override
    public void initialize() throws ParseException {

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq( Orders::getStatus, 1);
        List<Orders> ordersList = this.list(queryWrapper);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date now = new Date();

        for (int i = 0; i < ordersList.size(); i++) {

            Orders orders = ordersList.get(i);
            String useTime = orders.getUseTime();
            useTime = useTime + " 23:59:59";
            Date userDay = df.parse(useTime);
            if (now.after(userDay)){
                orders.setStatus(3);
                this.updateById(orders);
            }

        }

    }

    // 根据商品的 id, 批量修改订单的状态
    @Override
    public Boolean updateStatusByWaresId(Wares wares) throws ParseException {

        if (wares.getStatus() ==null || wares.getId() == null){
            System.out.println("根据商品的 id, 批量修改订单的状态1");

            return false;
        }

        System.out.println("根据商品的 id, 批量修改订单的状2");
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getWaresId, wares.getId());
        List<Orders> list = this.list(queryWrapper);

        // 把正常订单，和可取消的订单取消

            for (int i = 0; i < list.size(); i++) {
                Orders orders = list.get(i);
                if (orders.getStatus() == 1 && ! OrdersTime.lastDay(orders.getUseTime())){
                    orders.setStatus(0);
                    orders.setUpdateTime(LocalDateTime.now());
                    this.updateById(orders);
                }
            }

        return true;
    }

    @Override
    public List getEmployeeOrders(Long waresId, String userPhone,Integer status) {

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(waresId != null, Orders :: getWaresId, waresId);
        queryWrapper.like(userPhone != null && !userPhone.equals("-1"), Orders::getUserPhone, userPhone);
        queryWrapper.eq(status != null && status != 4, Orders :: getStatus, status);
        queryWrapper.orderByDesc(Orders::getUpdateTime);
        List<Orders> list = this.list(queryWrapper);


        return list;
    }


}
